package com.demandbreakdown.xiyun.model.vo.space;

import com.demandbreakdown.xiyun.model.entity.SpaceUser;
import com.demandbreakdown.xiyun.model.vo.user.UserVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 空间成员VO
 *
 * @author poxz
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "空间成员VO")
public class SpaceUserVO {

    @Schema(description = "用户ID", example = "123")
    private Long userId;

    @Schema(description = "用户名称", example = "alice")
    private String userName;

    @Schema(description = "用户角色：owner/editor/viewer", example = "owner")
    private String userRole;

    @Schema(description = "加入时间", example = "2025-09-20T12:00:00Z")
    private LocalDateTime joinTime;

    @Schema(description = "用户信息")
    private UserVO user;

    public static SpaceUserVO from(SpaceUser spaceUser, UserVO userVO) {
        if (spaceUser == null) {
            return null;
        }
        return SpaceUserVO.builder()
                .userId(spaceUser.getUserId())
                .userRole(spaceUser.getUserRole())
                .joinTime(spaceUser.getJoinTime())
                .user(userVO)
                .userName(userVO != null ? userVO.getUserName() : null)
                .build();
    }
}
